/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.StringTokenizer;

import cgl.narada.protocol.DataReceived;
import cgl.narada.protocol.Handler;
import cgl.narada.transport.TransportException;
import cgl.narada.transport.TransportHandler;
import cgl.narada.transport.TransportHandlerImpl;

/** This is an example which demonstrates using the transport interfaces in
    Narada. This class has presently been written for TCP communications. 
    Subsequent versions of this class would include support for other 
    transport types supported in NaradaBrokering.
    
    @author Shrideep Pallickara
    $Date$
    $Revision$
*/

public class CommunicationEndpoint implements Handler {
  String moduleName = "CommunicationEndpoint: ";
  

  /** Deals with the transport related aspects of communication */
  private TransportHandler transportHandler;


  public CommunicationEndpoint() {
    transportHandler = new TransportHandlerImpl(this);
  }

  public void loadCommunicationsOfType(Properties commProps, String commType) {
    try {
      transportHandler.loadCommunicationsOfType(commProps, commType);
    } catch (TransportException transEx) {
      System.out.println(transEx);
    }
  }
  
  public String createLinkTo(Properties props, String linkType) {
    try {
      return transportHandler.setupLink(props, linkType);
    } catch (TransportException transEx) {
      System.out.println(transEx);
    }
    return null;
  }
  

  public void sendDataTo(byte[] data, String node) {
    try {
      transportHandler.sendData(data, node);
    } catch (TransportException transEx) {
      System.out.println(transEx);
    }
  }

  /** This method indicates that the node in question is a client */
  public boolean isClient() {
    return true;
  }
  
  public void handleData(byte[] data, String sender) {

    if (data.length == 0) {
      System.out.println(moduleName + "Data received has length = 0");
      return;
    }
    
    //System.out.println(moduleName + "Received ->" + 
    //new String(_data, 0, _data.length) +" from " + _sender);
    System.out.println(moduleName + "Received Data size->" + data.length); 
  }


  public static void main(String[] args) {
    CommunicationEndpoint endpoint = new CommunicationEndpoint();
    Properties props = new Properties();
    props.put("TCPServerPort", args[0]);
    endpoint.loadCommunicationsOfType(props, "tcp");


    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tok;
    String cmd;
    while (true) {
      try {
	cmd = r.readLine();
	if(cmd.startsWith("h")) {
	  System.out.println("Create Link       -> cr hostname portNum ");
	  System.out.println("Send Data To         -> s  linkId data" );
	}

	if (cmd.startsWith("cr")) {
	  tok = new StringTokenizer(cmd, " ");
	  tok.nextToken(); /* skipping the token "cr" */
	  Properties linkProps = new Properties();
	  linkProps.put("hostname", tok.nextToken());
	  linkProps.put("portnum", tok.nextToken());
	  String _linkId = endpoint.createLinkTo(linkProps, "tcp");
	  System.out.println("Created link with id => " + _linkId); 
	  continue;
	} /** end create link */
	

	

	if (cmd.startsWith("s ")) {
	  tok = new StringTokenizer(cmd, " ");
	  tok.nextToken(); /* skipping the token "s" */
	  String _linkId = tok.nextToken();
	  String _toSend="";
	  while (tok.hasMoreTokens()) {
	    _toSend = (_toSend+ tok.nextToken() + " ");
	  }
	  //byte[] data = _toSend.getBytes();
	  byte[] data = new byte[50000];
	  for (int i=0; i< 400 ; i++)
	    endpoint.sendDataTo(data, _linkId);
	  continue;
	} /* end send data */
	

      }catch (IOException ioe) {
	ioe.printStackTrace();
      } /* end try-catch */
    } /* end while(true) */
    
  }/*end main() */
  
}
